﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using wpffc.contracts.domaindata;
using wpffc.contracts.infrastructure;
using wpffc.flashcardbox.domaindata;

namespace wpffc.flashcardbox
{
    public class Advance_card : FlashCardBoxEntity
    {
        public void Process(bool knewTheAnswer)
        {
            Trace.TraceInformation("Advance_card: {0}", knewTheAnswer);
            var box = base.boxContainer.Value;

            if (box.Batch.Status != BatchStatus.Empty)
            {
                var batchCard = box.Batch.Dequeue();
                if (knewTheAnswer)
                    Move_card_to_next_compartment(box, batchCard);
                else
                    Keep_card_in_batch(box, batchCard);
            }

            this.Continue(box);
        }


        private static void Move_card_to_next_compartment(IFlashCardBox box, BatchCard batchCard)
        {
            Trace.TraceInformation("Move card to next compartment");
            if (batchCard.CompartmentIndex < (box.Compartments.Length-1))
                box.Compartments[batchCard.CompartmentIndex + 1].Enqueue(batchCard.Card);
            else
                box.Archive.Enqueue(batchCard.Card);
        }


        private static void Keep_card_in_batch(IFlashCardBox box, BatchCard batchCard)
        {
            Trace.TraceInformation("Keep card in batch");
            box.Batch.Enqueue(batchCard);
        }


        public event Action<IFlashCardBox> Continue;
    }
}
